package com.mmf.web;

import com.mmf.dto.ResponseModel;
import com.mmf.model.*;
import com.mmf.service.ICommentService;
import com.mmf.service.IGoodsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by MMF on 2017-06-21.
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController {
    @Resource
    private IGoodsService goodsService;
    @Resource
    private ICommentService commentService;

    @RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
    @ResponseBody
//    private Object uploadFile(@RequestParam(value = "data",defaultValue = "",required = false) String data,@RequestParam(value = "params",defaultValue = "",required = false) Map<String, MultipartFile> params) {
//    private Object uploadFile(@RequestParam(value = "data",defaultValue = "",required = false) String data,Map<String, MultipartFile> params) {
    private Object uploadFile(@RequestPart(name = "data", value = "data", required = false) String data, @RequestBody Map<String, RequestBody> params) {
        List<String> list = new ArrayList<String>();
        list.add("sss");
//                = fileService.uploadFile();
//        model.addAttribute("list", list);
        // list.jsp + model = ModelAndView
        System.out.println("**********/////////" + data);
        return list;
    }

    /**
     * 插入一条商品详细信息
     *
     * @param goodsDetail
     * @return
     */
    @RequestMapping(value = "/insertGoods", method = RequestMethod.POST)
    @ResponseBody
    public Object insertGoods(GoodsDetail goodsDetail, MultipartHttpServletRequest request) throws IllegalStateException, IOException {
        return goodsService.insertGoods(goodsDetail, request);
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/getGoodsDetail", method = RequestMethod.GET)
    @ResponseBody
    public Object getGoodsDetail(Integer id, Integer userId) {
        ResponseModel model = new ResponseModel<List<GoodsDetailAndImg>>(0, "获取数据成功");
        if (StringUtils.isEmpty(id)) {
            model.setMessage("缺少参数id");
            return model;
        }
        Page page = new Page(1, 1);
        model = goodsService.getGoodsDetail(id, userId);
        if (model.getCode() != 0) {
            return model;
        } else {
            GoodsDetailAndImg goodsDetailAndImg = (GoodsDetailAndImg) model.getData();
            goodsDetailAndImg.setGoodsCommentList((List<Comment>) commentService.list(page, "", id + "").getData());
            return new ResponseModel<GoodsDetailAndImg>(0, "获取数据成功", goodsDetailAndImg);
        }
    }

    /**
     * 获取商品列表
     *
     * @return
     */
    @RequestMapping(value = "/getGoodsList", method = RequestMethod.GET)
    public String getGoodsList(Model model, @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) {
        Page page = new Page(pageNo,pageSize);
        List<GoodsList> list = goodsService.getGoodsList(page);
        model.addAttribute("list", list);
        return "GoodsList";
    }

    /**
     * 收藏商品
     *
     * @param userId
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/collect", method = RequestMethod.GET)
    @ResponseBody
    public Object collect(Integer userId, Integer goodsId) {
        return goodsService.collect(userId, goodsId);
    }

    /**
     * 取消收藏商品
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/cancelCollect", method = RequestMethod.GET)
    @ResponseBody
    public Object cancelCollect(Integer userId, Integer goodsId) {
        return goodsService.cancelCollect(userId, goodsId);
    }

    /**
     * 获取个人收藏列表
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getCollectList", method = RequestMethod.GET)
    @ResponseBody
    public Object getCollectList(Integer pageNo, Integer pageSize, Integer userId) {
        Page page = new Page(request);
        return goodsService.getCollectList(page, userId);
    }
}
